<load href="__JS__/easyui/jquery.easyui.min.js" />
<load href="__JS__/upload/jquery.uploadify.js" />
<script type="text/javascript">
    var thumbPath = '{$Think.config.UPLOAD_PATH_CATEGORY}';
    /**
     * 編輯
     */
    function treeEdit(row){
        if(row.category_id != 1) {
            $('.upload-img').html('');
            $('#baseForm').form('load',row);
            if(row.thumb) {
                $('.upload-img').html('<img src="'+SITE_URL+thumbPath.substring(1)+row.thumb+'" />');
            }
        } else {
            alert('Can\'t edit root');
        }
    }
    /**
     * 重新載入數據
     **/
    function _afterComplete(){
        $('#category-tree').treegrid('reload');
        $('#parent_id').combotree('reload');
        formReset();
    }
    
    function formReset(){
        $('.upload-img').html('');
        $('#baseForm').form('clear');
        $('#baseForm').form('load',{"status": 1,"position":0});
    }
    
    $(function(){
        $('#addCategory').live('click',formReset);
        $('#resetForm').live('click',formReset);
        $('#deleteCategory').live('click',function(){
            var row = $('#category-tree').treegrid('getSelected');
            if(!row) {
                alert('Please Select First');
            } else {
                /**
                 * 刪除
                 */
                $.post(
                    TP_ADMIN+'/Category/delete',
                    {category_id : row.category_id},
                    function(json){
                        if(json.status === 1) {
                            _afterComplete();	
                        } else {
                            alert(json.info);
                        }
                    },
                    'json'
                );
            }
        });
        uploadBase('thumb',thumbPath,'uploadCategory');
    });
</script>
<link href="__JS__/easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<div class="row-fluid">
    
    <div class="span6">
            <button class="btn btn-info " id="addCategory"><i class="icon-plus icon-white"></i></button>
            <button class="btn btn-danger date-delete " id="deleteCategory"><i class="icon-trash icon-white"></i></button>
           
        <br /><br />
        <table id="category-tree" title="{:L('common_category_tip')}" class="easyui-treegrid" style="width:600px;height:600px"  
            data-options="  
                url: '__ADMIN__/Category/getTree',  
                rownumbers: true,  
                idField: 'category_id',  
                treeField: 'category_name',
                onDblClickRow: treeEdit
            ">  
        <thead>  
            <tr>  
                <th data-options="field:'category_name'" width="240">{:L('common_category_name')}</th>  
                <th data-options="field:'identify'" width="100" align="right" formatter="showEmpty">{:L('common_category_identify')}</th>
                <th data-options="field:'position'" width="60" align="right">{:L('common_position')}</th>
                <th data-options="field:'status'" width="60" align="right" formatter="showStatus">{:L('common_category_status')}</th>
                <th data-options="field:'language'" width="60" align="right" formatter="showLanguage">{:L('common_language')}</th>
            </tr>  
        </thead>  
    </table>   
    </div>
    <div class="span6">
         <form class="form-horizontal" action="__URL__/insertCategory" method="post" id="baseForm">
                <fieldset>
                    <legend>{:L('common_category_form')}</legend>
                    
                    <div class="control-group">
                        <label class="control-label" for="category_name">{$Think.lang.common_name}</label>
                        <div class="controls">
                            <input class="input-xlarge focused" id="category_name" type="text" name="category_name" value="{$row.category_name}" required>
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <label class="control-label" for="parent_id">{$Think.lang.common_category_parent}</label>
                        <div class="controls">
                                <select id="parent_id" name="parent_id" class="easyui-combotree" style="width:200px;"  
            data-options="url:'__ADMIN__/Category/getCombotree',required:true"></select>  
                        </div>
                    </div>
                    
                    <include file="Public:language" />
                    <div class="control-group">
                        <label class="control-label" for="description">{$Think.lang.common_description}</label>
                        <div class="controls">
                            <textarea class="input-xlarge focused" id="description" name="description"></textarea>
                            
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="thumb">{$Think.lang.common_thumb}</label>
                        <div class="controls">
                            <input class="input-xlarge focused" id="thumb" type="text" name="thumb"> <a class="btn btn-danger delete-image btn-primary "><i class="icon-remove icon-white"></i></a>
                            <div class="upload-box">
                                <div class="upload-button">
                                    <input type="text" id="thumb_upload" />
                                </div>
                                <div class="upload-img thumbnail thumb_show"></div>
                                
                            </div>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="position">{$Think.lang.common_position}</label>
                        <div class="controls">
                            <input class="input-xlarge focused" id="position" type="text" name="position" value="{$row.position}">
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <label class="control-label" for="status">{$Think.lang.common_status}</label>
                        <div class="controls">
                           <label class="radio">
                            <input type="radio" name="status" id="optionsRadios1" value="1" checked>
                            {$Think.lang.common_on}
                            </label>
                            <label class="radio">
                            <input type="radio" name="status" id="optionsRadios2" value="0">
                            {$Think.lang.common_off}
                            </label>
                        </div>
                    </div>
                    <input type="hidden" name="category_id" id="categiry_id" />

                    <div class="form-actions">
                    <button type="submit" class="btn btn-primary form-submit">{$Think.lang.common_submit}</button>&nbsp;&nbsp;
                    <button class="btn" type="button" id="resetForm">{$Think.lang.common_reset}</button>
                </div>
                </fieldset>
            </form>
                    
    </div>
</div>

